<?php
class ReadTitle
{
    protected  $_filename;
    protected  $_text;
    protected  $_words;
    protected  $_wordsSkip = array(
        'in', 'at', 'aren\'t',
        'isn\'t', 'is', 'it', 'for', 'on', 'and', 'this', 'an', 'by', 'to', 'its',
        'it\'s', 'are', 'the', 'up', 'down', 'be', 'of',
    );

    function _read()
    {
        if (null === $this->_text) {
            if (!$this->_filename) {
                throw new Exception('File is not set.');
            }
            if (!file_exists($this->_filename)) {
                throw new Exception("File '{$this->_filename}' not found.");
            }
            $this->_text = file_get_contents($this->_filename);
            $this->_cutText();
        }
        return $this;
    }

    public function getText()
    {
        if (null === $this->_text) {
            $this->_read();
        }
        return $this->_text;
    }

    public function getWords()
    {
        if (null === $this->_words) {
            $this->_read();
            $this->_cutWord();
        }
        return $this->_words;
    }

    protected function _cutText()
    {
        $this->_text = explode(PHP_EOL, $this->_text);
        $parsed = '';
        foreach ($this->_text as $line) {
            $line = trim($line);
            if (empty($line) || false !== strpos($line, '<') || !preg_match('/[A-z]/', $line)) {
                continue;
            }
            $parsed .= $line . PHP_EOL;
        }
        $this->_text = $parsed;
        return $this;
    }

    protected function _cutWord()
    {
        $parsed = array();
        $this->_text = explode(PHP_EOL, $this->_text);
        foreach ($this->_text as $line) {
            preg_match_all('/((to |a |an |the |at ){0,1}[a-z\'-]{2,})\s/i', $line, $matches);
            foreach ($matches[0]as $word) {
                $word = strtolower(trim($word));
                foreach (array('a ', 'an ', 'the ') as $article) {
                    if (0 === stripos($word, $article)) {
                        $word = str_ireplace(array('a ', 'an ', 'the '), '', $word);
                        break;
                    }
                }
                if (substr($word, -3, 3) == "in'") {
                    $word = str_replace('in\'', 'ing', $word);
                }
                if (substr($word, -2, 2) == "'s") {
                    $word = str_replace("'s", '', $word);
                }

                if (false !== array_search($word, $this->_wordsSkip)) {
                    continue;
                }

                $parsed[$word] = 1;
            }
        }
        $this->_words = array_keys($parsed);
        return $this;
    }

    public function setFilename($filename)
    {
        $this->_filename = $filename;
        return $this;
    }

    public function writeFileWords()
    {
        $file = substr($this->_filename, 0, mb_strlen($this->_filename) - 4) . '_words.txt';
        $txt = '';
        foreach ($this->getWords() as $i) {
            $txt .= $i . PHP_EOL;
        }
        $fh = fopen($file, 'w') or die("Can't open file '$file'");
        fwrite($fh, $txt);
        fclose($fh);
        return $this;
    }

    public function writeCompare()
    {
        $name = $this->_getFirstName();
        $transFile = $name . '_trans.txt';
        $wordsFile = $name . '_words.txt';

        if (!file_exists($wordsFile)) {
            throw new Exception("File $wordsFile is not exist.");
        }
        if (!file_exists($transFile)) {
            throw new Exception("File $transFile is not exist.");
        }

        $words = file_get_contents($wordsFile);
        $wordsTr = file_get_contents($transFile);
        $words = explode(PHP_EOL, $words);
        $wordsTr = explode(PHP_EOL, $wordsTr);
        $delimiter = '|';
        $output = '';
        foreach ($wordsTr as $k => $word) {
            $word = trim($word);
            if (preg_match('/[A-z]/', $word) || empty($word)) {
                continue;
            }
            $output .= $words[$k] . $delimiter . $word . PHP_EOL;
        }

        $file = $name . '_merge.txt';
        $fh = fopen($file, 'w') or die("Can't open file");
        fwrite($fh, $output);
        fclose($fh);
        return $this;
    }

    protected function _getFirstName()
    {
        return substr($this->_filename, 0, mb_strlen($this->_filename) - 4);
    }
}
